﻿using ELTE.SEM.Data.Managers;
using ELTE.SEM.Data.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace ELTE.NPRG.WEB.Controllers
{
    public class SEMLogController : ControllerBase
    {
        LogEntryManager LogEntryManager { get; set; }
        ImageManager ImageManager { get; set; }
        UserManager UserManager { get; set; }
        FolderManager FolderManager { get; set; }

        public SEMLogController()
            : base()
        {
            this.LogEntryManager = new LogEntryManager(this.DataEntities);
            SemFileServerLoginViewModel loginViewModel = GetRemoteLoginViewModel();
            this.ImageManager = new ImageManager(loginViewModel, this.DataEntities);
            UserManager = new UserManager(this.DataEntities);
            FolderManager = new FolderManager(this.DataEntities);
        }

        public ActionResult Index()
        {
            var model = LogEntryManager.GetAllLogEntries();
            return View(model);
        }

        [HttpPost]
        public JsonResult AddLogEntry(SEMLogEntryViewModel model)
        {
            if (User.Identity.IsAuthenticated)
            {
                bool insertResult = LogEntryManager.InsertNewLogEntry(model, User.Identity.Name);
                if (insertResult)
                {
                    //ha sikeres volt a művelet, lekérjök az újonnan felvitte elemhez tartozó viewmodel-t
                    SEMLogEntryViewModel newest = LogEntryManager.GetNewest();
                    if (newest != null)
                    {
                        try
                        {
                            string result = RenderViewToString("_LogEntryPartial", newest);
                            return Json(new { Result = result }, JsonRequestBehavior.AllowGet);
                        }
                        catch (Exception ex)
                        {
                            //TODO: log error
                            return Json(new { ErrorMessage = "Hiba történt" }, JsonRequestBehavior.AllowGet);
                        }
                    }
                    else
                    {
                        return Json(new { ErrorMessage = "Nem található a legfrisebb elem" }, JsonRequestBehavior.AllowGet);
                        //TODO: log error
                    }
                }
                else
                {
                    return Json(new { ErrorMessage = "Nem sikerült a bejegyzés hozzáadása" }, JsonRequestBehavior.AllowGet);
                }
            }
            else
            {
                return Json(new { ErrorMessage = "Felhasználó nincs autentikálva" }, JsonRequestBehavior.AllowGet);
            }


        }

        [HttpPost]
        public JsonResult AddLogEntryPart(SEMLogEntryPartViewModel model, int logentryId)
        {
            if (LogEntryManager.InsertNewEntryPart(model, logentryId, User.Identity.Name))
            {
                return Json(RenderViewToString("_LogEntryPartPartial", LogEntryManager.GetNewestPart(logentryId)), JsonRequestBehavior.AllowGet);
            }
            else
            {
                return Json(new { }, JsonRequestBehavior.AllowGet);
            }
        }

        public ActionResult LogEntryPicturesList()
        {
            UserFoldersViewModel model = null;
            if (User.Identity.IsAuthenticated)
            {
                model = UserManager.GetUserFolderAccess(UserManager.GetUserId(User.Identity.Name),true);
            }
            else
            {
                return RedirectToAction("Index","Home");
            }
            return View(model);
        }

        public ActionResult LogEntryPictures(int logEntryId)
        {
            SEMLogEntryViewModel model = LogEntryManager.GetById(logEntryId);
            List<SEMLogEntryPictureViewModel> pictures = new List<SEMLogEntryPictureViewModel>();
            if (model != null)
            {
                pictures = this.ImageManager.GetImagesWithData(model);
            }
            return View(pictures);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="id">a listázni kívánt könyvtár id-je</param>
        /// <returns></returns>
        public ActionResult ScanImages(int id)
        {
            if (User.Identity.IsAuthenticated)
            {
                if (UserManager.HasAccess(User.Identity.Name, id))
                {
                    string folderPath = FolderManager.GetFolderFullPath(id);
                    List<SEMLogEntryPictureViewModel> picturesInFolder = this.ImageManager.GetImagesByFolder(folderPath);
                    return View(picturesInFolder);
                }
                else
                {
                    return RedirectToAction("Index", "Home");
                }
            }
            else
            {
                return RedirectToAction("Index", "Home");
            }
        }

        [HttpPost]
        public JsonResult CloseEntry(int entryId, DateTime closeDate)
        {
            if (LogEntryManager.CloseEntry(entryId, closeDate))
            {
                return Json("Success", JsonRequestBehavior.AllowGet);
            }
            else
            {
                return Json("Error", JsonRequestBehavior.AllowGet);
            }
        }

        [HttpPost]
        public JsonResult EditEntryPart(SEMLogEntryPartViewModel model)
        {
            if (LogEntryManager.EditEntryPart(model))
            {
                return Json(new { View = RenderViewToString("_LogEntryPartPartial", LogEntryManager.GetEntryPartViewModel(model.LogEntryId, model.Id)) }, JsonRequestBehavior.AllowGet);
            }
            else
            {
                return Json(new { Error = "Szerkesztés nem sikerült" }, JsonRequestBehavior.AllowGet);
            }
        }

        [HttpPost]
        public JsonResult DeleteEntryPart(SEMLogEntryPartViewModel model)
        {
            if (LogEntryManager.DeleteEntryPart(model))
            {
                return Json(new { Success = true }, JsonRequestBehavior.AllowGet);
            }
            else
            {
                return Json(new { Success = false, Error = "Törlés nem sikerült" }, JsonRequestBehavior.AllowGet);
            }
        }

        

    }
}
